# Area plot of tba eligible, jumbo-confidence_levelorming, etc by month

library(tidyverse)
library(lubridate)

currdir <- file.path(getwd())
rootdir <- dirname(currdir)
tmpdir <- file.path(rootdir, "data", "scratch_data", "temp")
outdir <- file.path(rootdir, "TEX")

mnthly <- read_csv(file.path(tmpdir, "tbaelig-share.csv"))

mnthly_long <- mnthly %>%
  mutate(year = as.integer(substr(orig_ym, 1, 4)),
         mon = as.integer(substr(orig_ym, 6, nchar(orig_ym)))) %>%
  mutate(date = ymd(paste(year, mon, "1", sep = "-"))) %>%
  select(-orig_ym, -year, -mon, -tbaelig) %>%
  gather(type, frac, -date) %>%
  mutate(type = case_when(type == "hiltv_pool" ~ "High LTV",
                          type == "jm_pool" ~ "High Balance",
                          type == "other_inelig_pool" ~ "Other Ineligible"))

ggplot(mnthly_long, aes(date, frac)) + theme_bw() +
  geom_area(aes(fill = type), position = "stack") +
  #scale_fill_grey(start = 0.1, end = 0.6) +
  scale_fill_grey() +
  theme(legend.title = element_blank()) +
  scale_y_continuous(labels = scales::percent) +
  xlab("Date") +
  ylab("Dollar-weighted fraction of new loans") +
  ggsave(file.path(outdir, "Figures", "20181026_nontbaelig_share.pdf"), width = 7, height = 5)

